//
//  mainTableViewController.m
//  AddressBook
//
//  Created by 苗钧豪 on 16/8/5.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "mainTableViewController.h"
#import "mainTableViewCell.h"
#import "Friends.h"
#import "friendViewController.h"

@interface mainTableViewController ()

@end

@implementation mainTableViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//    NSString *filePath = [[array firstObject] stringByAppendingPathComponent:@"friends"];
    
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _Arr = [NSMutableArray array];
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    Friends *friends = [Friends new];
    friends.Name = @"李狗蛋";
    friends.Phone = @"15800111234";
    friends.City = @"江苏省南京市狗蛋街道110号";
    UIImage *image = [UIImage imageNamed:@"1"];
    friends.imgData = UIImagePNGRepresentation(image);
    NSString *filePath = [[array firstObject] stringByAppendingPathComponent:@"friends"];
    [NSKeyedArchiver archiveRootObject:friends toFile:filePath];
    Friends *friend = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    [_Arr addObject:friend];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _Arr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    Friends *Friend = [Friends new];
    Friend = _Arr[indexPath.row];
    UIImage *image = [UIImage imageWithData:Friend.imgData];
    cell.nameLabel.text = Friend.Name;
    cell.phoneNumber.text = Friend.Phone;
    cell.cityLabel.text = Friend.City;
    cell.imgView.image = image;
    // Configure the cell...
    
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [_Arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier  isEqual: @"123"]) {
        mainTableViewCell *cell = (mainTableViewCell *)sender;
        NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
        friendViewController *vc = segue.destinationViewController;
        vc.friends = _Arr[indexpath.row];
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
